﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using WinLib.Common;
namespace YNLibWin
{
    public partial class frmMain : Form
    {
        private int? _recflag = 0;
        private int? _userflag = 2;
        private int? _userrole = 2;

        /// <summary>
        /// 用户状态 1：未逻辑删除。0：逻辑删除
        /// </summary>
        public int? RECFLAG
        {
            set { _recflag = value; }
            get { return _recflag; }
        }
        /// <summary>
        /// 状态 1：未审核；2：已审核；3：停用；
        /// </summary>
        public int? UserFlag
        {
            set { _userflag = value; }
            get { return _userflag; }
        }
        /// <summary>
        /// 用户身份：1，管理员；2，普通用户；3，集团用户
        /// </summary>
        public int? UserRole
        {
            set { _userrole = value; }
            get { return _userrole; }
        }

        public frmMain()
        {
            InitializeComponent();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            InitInfo();
            BindData();
        }
        /// <summary>
        /// 初始化页面
        /// </summary>
        protected void InitInfo()
        {
            #region 绑定用户检索字段
            ComboBoxItem cbi1 = new ComboBoxItem();
            cbi1.Text = "用户账号";
            cbi1.Value = "登录账号";
            this.cboSearchField.Items.Add(cbi1);
            ComboBoxItem cbi2 = new ComboBoxItem();
            cbi2.Text = "用户姓名";
            cbi2.Value = "用户姓名";
            this.cboSearchField.Items.Add(cbi2);
            this.cboSearchField.SelectedIndex = 0; 
            #endregion
        }
        private void BindData()
        {
            BindData("");
        }
        private void BindData(string filter)
        {
            string sqlstr = string.Format("select SYSID as ID,UserCode as '登录账号',RealName as '用户姓名',UserFlag as '当前状态',Email,JoinDate as '开户时间',EndDate as '到期时间' from [SSO_UserBaseInfo] where UserRole=" + _userrole.ToString() + " and RECFLAG=1");
            DataTable dt = WinLib.DBUtility.DbHelperSQL.ExecuteQuery(sqlstr);
            if (!string.IsNullOrEmpty(filter))
            {
                DataTable newdt = new DataTable();
                newdt = dt.Clone();
                DataRow[] dr = dt.Select(filter);
                for (int i = 0; i < dr.Length; i++)
                {
                    newdt.ImportRow((DataRow)dr[i]);
                }
                this.dataGridView1.DataSource = newdt;
            }
            else
            {
                this.dataGridView1.DataSource = dt;
            }

            this.lblCount.Text = "共" + dt.Rows.Count.ToString() + "条记录";
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
                return;
            int ID = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            frmUserAddEdit fUserAddEdit = new frmUserAddEdit();
            switch (_userrole)
            {
                case 1:
                    {
                        fUserAddEdit.SYSID = ID;
                        fUserAddEdit.IsAdmin = true;
                        fUserAddEdit.Show();
                    }
                    break;
                case 2:
                    {
                        fUserAddEdit.SYSID = ID;
                        fUserAddEdit.Show();
                    }
                    break;
                case 3:
                    {
                        frmGroupAddEdit fGroupAddEdit = new frmGroupAddEdit();
                        fGroupAddEdit.SYSID = ID;
                        fGroupAddEdit.Show();
                    }
                    break;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
                return;
            if (MessageBox.Show("确认删除选中的行吗？删除后可在回收站中恢复", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int intResult = 0;
                int intCountRows = this.dataGridView1.Rows.Count;
                for (int i = this.dataGridView1.SelectedRows.Count; i > 0; i--)
                {
                    int ID = Convert.ToInt32(dataGridView1.SelectedRows[i - 1].Cells[0].Value);
                    dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i - 1].Index);//在DataGridView中移除删除的行
                    //逻辑删除记录
                    string sqlstr = string.Format("Update [SSO_UserBaseInfo] set RECFLAG=2 where SYSID=" + ID.ToString());
                    int intDel = Convert.ToInt32(WinLib.DBUtility.DbHelperSQL.ExecuteNonQuery(sqlstr)); //cl是操作类的一个对像，Execute()是类中的一个方法
                    if (intDel > 0)
                    {
                        intResult += intDel;
                    }
                }
                int intCount = intCountRows - intResult;
                this.lblCount.Text = "共" + intCount.ToString() + "条记录";
                MessageBox.Show("成功删除" + intResult.ToString() + "条记录", "删除记录", MessageBoxButtons.OK);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmUserAddEdit fUserAddEdit = new frmUserAddEdit();
            switch (_userrole)
            {
                case 1:
                    {
                        fUserAddEdit.IsAdmin = true;
                        fUserAddEdit.Show();
                    }
                    break;
                case 2:
                    {
                        fUserAddEdit.Show();
                    }
                    break;
                case 3:
                    {
                        frmGroupAddEdit fGroupAddEdit = new frmGroupAddEdit();
                        fGroupAddEdit.Show();
                    }
                    break;
            }
        }

        private void menu1_Click(object sender, EventArgs e)
        {
            _userrole = 2;
            BindData();
            this.grpUserList.Text = "普通用户列表";
        }

        private void menu2_Click(object sender, EventArgs e)
        {
            _userrole = 3;
            BindData();
            this.grpUserList.Text = "集团用户列表";
        }

        private void menu3_Click(object sender, EventArgs e)
        {
            _userrole = 1;
            BindData();
            this.grpUserList.Text = "管理员列表";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtKeyword.Text))
            {
                MessageBox.Show("请输入检索关键词");
                return;
            }
            string strField = Convert.ToString(((ComboBoxItem)this.cboSearchField.SelectedItem).Value);
            string strFilter = strField + " like '%" + this.txtKeyword.Text.Trim() + "%'";
            BindData(strFilter);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmRecycle fRecycle = new frmRecycle();
            fRecycle.Show();
        }

        /// <summary>
        /// 双击后打开编辑窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //e.ColumnIndex是双击的当前列的索引，e.RowIndex则是双击的当前行的索引
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                int ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                frmUserAddEdit fUserAddEdit = new frmUserAddEdit();
                switch (_userrole)
                {
                    case 1:
                        {
                            fUserAddEdit.SYSID = ID;
                            fUserAddEdit.IsAdmin = true;
                            fUserAddEdit.Show();
                        }
                        break;
                    case 2:
                        {
                            fUserAddEdit.SYSID = ID;
                            fUserAddEdit.Show();
                        }
                        break;
                    case 3:
                        {
                            frmGroupAddEdit fGroupAddEdit = new frmGroupAddEdit();
                            fGroupAddEdit.SYSID = ID;
                            fGroupAddEdit.Show();
                        }
                        break;
                }
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //单元格右击
                if (e.RowIndex >= 0)
                {
                    //将被右击的行设为选中状态
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                    //只选中一行时设置活动单元格
                    if (dataGridView1.SelectedRows.Count == 1)
                    {
                        if (e.ColumnIndex == -1)
                        {
                            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
                        }
                        else
                        {
                            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        }
                    }
                    //弹出操作菜单
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }
        /// <summary>
        /// 右键菜单编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
                return;
            int ID = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            frmUserAddEdit fUserAddEdit = new frmUserAddEdit();
            switch (_userrole)
            {
                case 1:
                    {
                        fUserAddEdit.SYSID = ID;
                        fUserAddEdit.IsAdmin = true;
                        fUserAddEdit.Show();
                    }
                    break;
                case 2:
                    {
                        fUserAddEdit.SYSID = ID;
                        fUserAddEdit.Show();
                    }
                    break;
                case 3:
                    {
                        frmGroupAddEdit fGroupAddEdit = new frmGroupAddEdit();
                        fGroupAddEdit.SYSID = ID;
                        fGroupAddEdit.Show();
                    }
                    break;
            }
        }
        /// <summary>
        /// 右键菜单删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
                return;
            if (dataGridView1.CurrentCell == null)
                return;
            int pintRowIndex = dataGridView1.CurrentCell.RowIndex;
            if (pintRowIndex != -1)
            {
                if (MessageBox.Show("确定要删除此行吗？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    //删除数据库中的记录
                    int intCountRows = this.dataGridView1.Rows.Count;
                    object pobjeid = dataGridView1.Rows[pintRowIndex].Cells[0].Value;
                    if (pobjeid != null && int.Parse(pobjeid.ToString()) != 0)
                    {
                        int ID = Convert.ToInt32(dataGridView1.Rows[pintRowIndex].Cells[0].Value);
                        if (ID > 0)
                        {
                            //逻辑删除记录
                            string sqlstr = string.Format("Update [SSO_UserBaseInfo] set RECFLAG=2 where SYSID=" + ID.ToString());
                            int intDel = Convert.ToInt32(WinLib.DBUtility.DbHelperSQL.ExecuteNonQuery(sqlstr)); //cl是操作类的一个对像，Execute()是类中的一个方法
                            if (intDel > 0)
                            {
                                dataGridView1.Rows.RemoveAt(pintRowIndex);//在DataGridView中移除删除的行
                                int intCount = intCountRows - intDel;
                                this.lblCount.Text = "共" + intCount.ToString() + "条记录";
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 右键菜单全选事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.SelectAll();
        }

        private void 添加集团用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmGroupAddEdit fGroupAddEdit = new frmGroupAddEdit();
            fGroupAddEdit.Show();
        }

        private void 添加普通用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmUserAddEdit fUserAddEdit = new frmUserAddEdit();
            fUserAddEdit.Show();
        }

        private void 添加管理员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmUserAddEdit fUserAddEdit = new frmUserAddEdit();
            fUserAddEdit.IsAdmin = true;
            fUserAddEdit.Show();
        }

    }
}
